package blue.mqtt4j.core.util;

import io.netty.handler.codec.mqtt.MqttFixedHeader;
import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageType;
import io.netty.handler.codec.mqtt.MqttProperties;
import io.netty.handler.codec.mqtt.MqttPubReplyMessageVariableHeader;
import io.netty.handler.codec.mqtt.MqttQoS;

/**
 * @author Jin Zheng
 * @since 1.0 2020-11-25
 */
public class MqttMessageUtil
{
	private MqttMessageUtil()
	{
	}

	public static MqttMessage createPubRec(int messageId)
	{
		return createPubRec(messageId, (byte)0, null);
	}

	public static MqttMessage createPubRec(int messageId, byte reasonCode, MqttProperties properties)
	{
		MqttFixedHeader fixedHeader =
				new MqttFixedHeader(MqttMessageType.PUBREC, false, MqttQoS.AT_LEAST_ONCE, false, 0);
		MqttPubReplyMessageVariableHeader variableHeader =
				new MqttPubReplyMessageVariableHeader(messageId, reasonCode, properties);
		return new MqttMessage(fixedHeader, variableHeader);
	}

	public static MqttMessage createPubRel(int messageId)
	{
		return createPubRel(messageId, (byte)0, null);
	}

	public static MqttMessage createPubRel(int messageId, byte reasonCode, MqttProperties properties)
	{
		MqttFixedHeader fixedHeader =
				new MqttFixedHeader(MqttMessageType.PUBREL, false, MqttQoS.AT_LEAST_ONCE, false, 0);
		MqttPubReplyMessageVariableHeader variableHeader =
				new MqttPubReplyMessageVariableHeader(messageId, reasonCode, properties);
		return new MqttMessage(fixedHeader, variableHeader);
	}

	public static MqttMessage createPubComp(int messageId)
	{
		return createPubComp(messageId, (byte)0, null);
	}

	public static MqttMessage createPubComp(int messageId, byte reasonCode, MqttProperties properties)
	{
		MqttFixedHeader fixedHeader =
				new MqttFixedHeader(MqttMessageType.PUBCOMP, false, MqttQoS.AT_MOST_ONCE, false, 0);
		MqttPubReplyMessageVariableHeader variableHeader =
				new MqttPubReplyMessageVariableHeader(messageId, reasonCode, properties);
		return new MqttMessage(fixedHeader, variableHeader);
	}

}
